package ui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MainView extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private final int WINDOW_WIDTH = 800;
	private final int WINDOW_HIGHT = 500;
	private final String WINDOW_TITLE = "Insing Model";
	
	private JPanel jContentPane = null;
	private JTextField jGridSizeTextField = null;
	private JButton jStartButton = null;
	private JTextField jTemperatureTextField = null;
	private JLabel jTimeValueLabel = null;
	private GraphViewIsingModelThread graphViewIsingModelRunable;
	private Thread graphViewIsingModelThread;

	/**
	 * This is the default constructor
	 */
	public MainView() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(WINDOW_WIDTH, WINDOW_HIGHT);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle(WINDOW_TITLE);
		
		addComponentsToPane(getJContentPane());
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
		}
		return jContentPane;
	}
	
	
	private void addComponentsToPane(Container pane)
	{
		JButton button;
		JLabel jLabel;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		
		graphViewIsingModelRunable = new GraphViewIsingModelThread();
		//graphViewIsingModelThread = new Thread(graphViewIsingModelRunable, "GraphViewIsingModelThread");
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridheight = 3;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		pane.add(graphViewIsingModelRunable, gridBagConstraints);
		
		
		jLabel = new JLabel();
		jLabel.setText("Rozmiar siatki: ");
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		pane.add(jLabel, gridBagConstraints);
		
		jGridSizeTextField = new JTextField();
		//jGridSizeTextField.addPropertyChangeListener("value", gridSizeTextFieldListener);
		jGridSizeTextField.addKeyListener(gridSizeTextFieldListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		pane.add(jGridSizeTextField, gridBagConstraints);
	
		jStartButton = new JButton("Start");
		jStartButton.addActionListener(actionStartButtonListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 1;
		pane.add(jStartButton, gridBagConstraints);
		
		button = new JButton("Stop");
		button.addActionListener(actionStopButtonListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 1;
		pane.add(button, gridBagConstraints);
		
		jLabel = new JLabel();
		jLabel.setText("Temperatura: ");
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 2;
		pane.add(jLabel, gridBagConstraints);
		
		jTemperatureTextField = new JTextField();
		jTemperatureTextField.getDocument().addDocumentListener(temperatureTextFieldListener);
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 4;
		gridBagConstraints.gridy = 2;
		pane.add(jTemperatureTextField, gridBagConstraints);
		
		button = new JButton("Init Cold");
		button.addActionListener(actionStopButtonListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 3;
		pane.add(button, gridBagConstraints);
		
		button = new JButton("Init hot");
		button.addActionListener(actionStopButtonListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 3;
		pane.add(button, gridBagConstraints);
		
		button = new JButton("Init warm");
		button.addActionListener(actionStopButtonListener);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.gridx = 4;
		gridBagConstraints.gridy = 3;
		pane.add(button, gridBagConstraints);
		
		jLabel = new JLabel();
		jLabel.setText("Czas: ");
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.0;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 4;
		pane.add(jLabel, gridBagConstraints);
		
		jTimeValueLabel = new JLabel();
		jTimeValueLabel.setText(" 0 [s]");
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 4;
		pane.add(jTimeValueLabel, gridBagConstraints);
		
	}
	
	public void updateTime(long timeValue)
	{
		jTimeValueLabel.setText(timeValue + " [s]");
	}
	
	private KeyListener gridSizeTextFieldListener = new KeyListener() {
		
		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			Integer value = Integer.parseInt(jGridSizeTextField.getText()) * 2;
			jGridSizeTextField.setText(value.toString());
			
		}
		
		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
	};
	
	private DocumentListener temperatureTextFieldListener = new DocumentListener() {

		@Override
		public void changedUpdate(DocumentEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void insertUpdate(DocumentEvent arg0) {
			// TODO Auto-generated method stub
			gridSizeTextFieldListener.keyReleased(null);
		}

		@Override
		public void removeUpdate(DocumentEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		

	};
	
	private ActionListener actionStartButtonListener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jStartButton.setText("Pause");
			
		}
	};
	
	private ActionListener actionStopButtonListener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}
	};
	
	private ActionListener actionChangeTemperatureListener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}
	};

}
